package org.mili.ticketeer.ui.priority;

import org.mili.ticketeer.dto.Priority;
import org.mili.ticketeer.service.PriorityService;
import org.mili.ticketeer.service.ServiceException;
import org.mili.ticketeer.service.ServiceFactory;
import org.mili.ticketeer.ui.SimpleTableModel;

import java.util.List;

/**
 */
public class PriorityTableModel extends SimpleTableModel<Priority> {
    private PriorityService _priorityService = ServiceFactory.getService(PriorityService.class);

    public PriorityTableModel() {
        super(new String[]{"No.", "Name", "Priority"});
        update();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Priority priority = get(rowIndex);
        switch(columnIndex) {
            case 0:
                return rowIndex + 1;
            case 1:
                return priority.getName();
            case 2:
                return priority.getPriority();
            default: throw new IllegalStateException("no column index for: " + columnIndex);
        }
    }

    @Override
    public List<Priority> load() {
        try {
            return _priorityService.getPriorities();
        } catch (ServiceException e) {
            throw new IllegalStateException(e);
        }
    }
}
